import { Tween, Node, tween, UIOpacity } from "cc";

export default class TweenTools {

    //渐变显示/隐藏
    static _tmpOpacity = { a: 0 };
    public static showUIFade(ui: Node, isShow, time = 0.2, afterCb?) {

        Tween.stopAllByTarget(this._tmpOpacity);
        let cmp = ui.getComponent(UIOpacity);
        if (!cmp) {
            cmp = ui.addComponent(UIOpacity);
        }
        if (isShow) {
            this._tmpOpacity.a = 0;
            ui.active = true;
            tween(this._tmpOpacity).to(time, { a: 255 }, {
                onUpdate: () => {
                    cmp.opacity = this._tmpOpacity.a;
                }
            }).call(() => {
                afterCb && afterCb();
            }).start();
        } else {
            tween(this._tmpOpacity).to(time, { a: 0 }, {
                onUpdate: () => {
                    cmp.opacity = this._tmpOpacity.a;
                }
            }).call(() => {
                ui.active = false;
                afterCb && afterCb();
            }).start();
        }

    }

}